﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using System.Collections.Specialized;

namespace SaturniLibrary.Web.Log
{
    public class WebLogFilter : IActionFilter, IExceptionFilter 
    {
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            try
            {

                HttpContext context = filterContext.HttpContext.ApplicationInstance.Context;

                ThreadLogging.GetLogger().Clear();

                ThreadLogging.GetLogger().StartStopWatch();

                ThreadLogging.GetLogger().Log("Begin Request");

                ThreadLogging.GetLogger().Log("Host=" + Environment.MachineName);
                ThreadLogging.GetLogger().Log("Request Time=" + DateTime.Now);
                ThreadLogging.GetLogger().Log("Request URL=" + context.Request.RawUrl);
                ThreadLogging.GetLogger().Log("Request Action=" + context.Request.HttpMethod);

                NameValueCollection headers = context.Request.Headers;
                foreach (string header in headers)
                {
                    ThreadLogging.GetLogger().Log("Http Header, name=" + header + ", value=" + headers[header]);
                }
            }
            catch
            {
            }
        }

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            try
            {
                HttpContext context = filterContext.HttpContext.ApplicationInstance.Context;
                WebLogHelper.LogRequestInfo(context.Request);
                WebLogHelper.LogSession(ThreadLogging.GetLogger().Session);

                ThreadLogging.GetLogger().Log("End Request Time=" + DateTime.Now);
                ThreadLogging.GetLogger().Log("End Request");

                ThreadLogging.GetLogger().GetStopWatch().Stop();
                ThreadLogging.GetLogger().Log("Page total execution time = " + ThreadLogging.GetLogger().GetStopWatch().ElapsedMs() + " ms");

                if (WebLogConfig.Instance.LogEnable)
                {
                    WebLogHelper.SaveExceptionLog(ThreadLogging.GetLogger().Exception, context);
                }
            }
            catch
            {
            }
            finally
            {
                ThreadLogging.GetLogger().Clear();
            }
        }

        public void OnException(ExceptionContext filterContext)
        {
            try
            {

                    ThreadLogging.GetLogger().GetStopWatch().Stop();
                    ThreadLogging.GetLogger().Log("Request Error Time=" + DateTime.Now);
                    ThreadLogging.GetLogger().Log("Page total execution time = " + ThreadLogging.GetLogger().GetStopWatch().ElapsedMs() + " ms");

                    //Considering logging session data as well
                    HttpContext context = filterContext.HttpContext.ApplicationInstance.Context;
                    Exception exception = filterContext.Exception;
                    if (exception.InnerException != null)
                    {
                        exception = exception.InnerException;
                    }
                    WebLogHelper.LogRequestInfo(context.Request);
                    WebLogHelper.LogSession(context.Session);

                    WebLogHelper.SaveExceptionLog(exception, context);
                
            }
            catch
            {
            }
            finally
            {
                ThreadLogging.GetLogger().Clear();
            }
        }
    }
}
